var common = require("../../../../../utils/common.js");
var app = getApp();
Page({
    data: {
        dateFrom:"< 2017-03-01",
        dateTo:"2017-03-31 >",
        kind:null,
        total:null,
        month:null,
        type:null,
        member:null,
        paramTitle:null,
        paramName:null,
        recordList:[],
        clickId:""//用于长按点击时，修改单项的颜色
    },
    onLoad:function(options){
        var that=this
        var paramTitle=null
        var paramName=null
        var type = ''
        var member = ''
        if(options.type){
            paramTitle='类别'
            paramName=options.type
            type=options.type
        }else{
            paramTitle='成员'
            paramName=options.member
            member=options.member
        }
        that.setData({
            kind:options.kind,
            month:options.month,
            type:type,
            member:member,
            paramTitle:paramTitle,
            paramName:paramName
        })
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration:10000
        })
        that.getDetailType(function(res){
            wx.hideToast()
        })
    },
    recordChange: function(e){
        var that=this
        if(e.currentTarget.dataset.month == 'previousmonth'){
            that.setData({month:--that.data.month})
        }else if(e.currentTarget.dataset.month == 'nextmonth'){
            that.setData({month:++that.data.month})
        }
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration:10000
        })
        that.getDetailType(function(res){
            wx.hideToast()
        })
    },
    getDetailType: function(cb) {
        var that=this
        // console.log('type----'+that.data.type)
        // console.log('member----'+that.data.member)
        wx.request({
            url: common.apiUrl()+'detailOfType',
            data: {
                token:app.globalData.wangcaiToken,
                kind:that.data.kind,
                type:that.data.type,
                member:that.data.member,
                month:that.data.month
            },
            method:'POST',
            header: {
            'content-type': 'application/x-www-form-urlencoded'
                },
            success: function(res){
                if(res.data.status==1){
                    // console.log(res.data.data.total)
                    that.setData({
                        recordList:res.data.data.detailtype,
                        dateFrom:"< "+res.data.data.daybegin,
                        dateTo:res.data.data.dayend+" >",
                        total:res.data.data.total
                    })
                typeof cb == "function" && cb(res.data)
                }else{
                app.errorToGetToken()
                }
            }
        })
    },
    recordDetail: function(e) {
    if(this.endTime  - this.startTime < 350) {
        if(e.currentTarget.dataset.kind<3){
            wx.navigateTo({
                url: '/pages/smartlife/wangcai/addrecord/addrecord?id='+e.currentTarget.dataset.id+'&kind='+e.currentTarget.dataset.kind
                })
            }
        
        }
    },
    bindTouchStart: function(e) {
        this.startTime = e.timeStamp;
    },
    bindTouchEnd: function(e) {
        this.endTime = e.timeStamp;
    },
    deleteRecord: function(e) {
        if(e.currentTarget.dataset.name.indexOf("转")!=-1){
            var that = this
            that.setData({
            clickId:e.currentTarget.dataset.id
            })
            wx.showActionSheet({
                itemList: ['删除'],
                itemColor: '#E64340',
                success: function(res) {
                    switch(res.tapIndex){
                    case 0:
                        var kind = "transfer"
                        wx.request({
                        url: common.hostUrl+'wangcai/'+ kind +'/delete',
                        data: {
                            ids:e.currentTarget.dataset.id
                        },
                        method:'POST',
                        header: {
                        'content-type': 'application/x-www-form-urlencoded'
                            },
                        success: function(res){
                            that.setData({
                            clickId:""
                            })
                            if(res.data.status==1){
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                success: function(e){
                                    // 刷新数据
                                    that.getDetailType(function(res){
                                        wx.hideToast()
                                    })
                                }
                            })
                            }else{
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'success'
                            })
                            }
                        }
                        })
                    break;
                    case 1:

                    break;
                    }
                },
                fail: function(res) {
                    that.setData({
                    clickId:""
                    })
                }
            })
        }
    },
    // 添加或编辑记录的回调，要和主界面函数名一致
    getData: function() {
        var that=this
        that.getDetailType(function(res){
            //刷新上一页数据
            // 当前页一直是5
          var pages = getCurrentPages();
          if(pages.length > 1){
              //上一个页面实例对象
              var prePage = pages[pages.length - 3];
            //   调用totalrecord界面的getTotalData方法，刷新totalrecord界面数据
              prePage.getTotalData()
          }
        })
    },
    btnTransfer:function(e){
        var that=this
        wx.navigateTo({
                url: '/pages/smartlife/wangcai/transfer/transfer?outaccount='+that.data.type
                })
    }
})